/*
 * FrmFatura.java
 *
 * Created on October 7, 2009, 1:06 AM
 */

package br.com.pangalacteos.apresentacao;

import br.com.pangalacteos.dao.HibernateDAO;
import br.com.pangalacteos.modelo.Fatura;
import br.com.pangalacteos.modelo.Fornecedor;
import br.com.pangalacteos.util.Parser;
import br.com.pangalacteos.util.Validator;
import javax.swing.JOptionPane;

/**
 *
 * @author  Leandro Del Sole
 */
public class FrmFatura extends javax.swing.JPanel {

    Fatura fatura = null;
    
    /** Creates new form FrmFatura */
    public FrmFatura() {
        initComponents();
        lblFatura.setVisible(false);
        lblIdFatura.setVisible(false);
    }
    
    public FrmFatura(Fatura fatura){
        this.fatura = fatura;
        initComponents();
        preencheCampos(fatura);
        if (fatura.getCdstatus().equalsIgnoreCase("N")){
            btnEstornar.setEnabled(true);
        }
        if (fatura.getCdstatus().equalsIgnoreCase("P")){
            btnAnular.setEnabled(true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblFatura = new javax.swing.JLabel();
        lblIdFatura = new javax.swing.JLabel();
        txtFornecedor = new javax.swing.JTextField();
        txtReferencia = new javax.swing.JTextField();
        txtMontante = new javax.swing.JTextField();
        txtDesconto = new javax.swing.JTextField();
        txtNotaFiscal = new javax.swing.JTextField();
        lblFornecedor = new javax.swing.JLabel();
        lblDataFatura = new javax.swing.JLabel();
        lblReferencia = new javax.swing.JLabel();
        lblMontante = new javax.swing.JLabel();
        lblVencimento = new javax.swing.JLabel();
        lblDesconto = new javax.swing.JLabel();
        lblVencimentoDesconto = new javax.swing.JLabel();
        lblDescricao = new javax.swing.JLabel();
        lblNotaFiscal = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        lblDataPagamento = new javax.swing.JLabel();
        btnSalvar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescricao = new javax.swing.JTextArea();
        txtDataFatura = new javax.swing.JFormattedTextField();
        txtVencimentoDesconto = new javax.swing.JFormattedTextField();
        txtVencimento = new javax.swing.JFormattedTextField();
        txtDataPagamento = new javax.swing.JFormattedTextField();
        cmbStatus = new javax.swing.JComboBox();
        lblNotaFiscal1 = new javax.swing.JLabel();
        lblDataPagamento1 = new javax.swing.JLabel();
        txtSerieNotaFiscal = new javax.swing.JTextField();
        txtDuplicata = new javax.swing.JTextField();
        btnEstornar = new javax.swing.JButton();
        btnAnular = new javax.swing.JButton();

        lblFatura.setText("Fatura N°");

        lblIdFatura.setText("000000");

        txtReferencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtReferenciaActionPerformed(evt);
            }
        });

        txtNotaFiscal.setEnabled(false);

        lblFornecedor.setText("Fornecedor");

        lblDataFatura.setText("Data da fatura");

        lblReferencia.setText("Referência");

        lblMontante.setText("Montante");

        lblVencimento.setText("Vencimento");

        lblDesconto.setText("Desconto");

        lblVencimentoDesconto.setText("Vencimento Desconto");

        lblDescricao.setText("Descrição");

        lblNotaFiscal.setText("Nota Fiscal");

        lblStatus.setText("Status");

        lblDataPagamento.setText("Data Pagamento");

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        txaDescricao.setColumns(20);
        txaDescricao.setLineWrap(true);
        txaDescricao.setRows(5);
        jScrollPane1.setViewportView(txaDescricao);

        try {
            txtDataFatura.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            txtVencimentoDesconto.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            txtVencimento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            txtDataPagamento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtDataPagamento.setEnabled(false);

        cmbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "N", "P", "E" }));
        cmbStatus.setEnabled(false);

        lblNotaFiscal1.setText("Série N.F.");

        lblDataPagamento1.setText("Duplicata");

        txtSerieNotaFiscal.setEnabled(false);

        txtDuplicata.setEnabled(false);

        btnEstornar.setText("Estornar");
        btnEstornar.setEnabled(false);
        btnEstornar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEstornarActionPerformed(evt);
            }
        });

        btnAnular.setText("Anular");
        btnAnular.setEnabled(false);
        btnAnular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnularActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(lblNotaFiscal1)
                                            .addComponent(lblReferencia)
                                            .addComponent(lblMontante)
                                            .addComponent(lblDesconto)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(lblNotaFiscal)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addGap(5, 5, 5)
                                                    .addComponent(lblFatura))))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(lblIdFatura)
                                                .addGap(12, 12, 12))
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(txtSerieNotaFiscal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                                                    .addComponent(txtReferencia, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                                                    .addComponent(txtMontante, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                                                    .addComponent(txtDesconto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                                                    .addComponent(txtNotaFiscal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
                                                .addGap(4, 4, 4)))
                                        .addGap(2, 2, 2)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                            .addComponent(lblVencimento)
                                                            .addComponent(lblVencimentoDesconto))
                                                        .addGap(9, 9, 9))
                                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                            .addComponent(lblDataPagamento1)
                                                            .addComponent(lblDataPagamento))
                                                        .addGap(6, 6, 6)))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(lblFornecedor)
                                                    .addComponent(lblDataFatura))
                                                .addGap(10, 10, 10))))
                                    .addComponent(lblDescricao))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtFornecedor, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                                    .addComponent(txtDataFatura, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtVencimentoDesconto, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtVencimento, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtDataPagamento, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblStatus)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmbStatus, 0, 80, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtDuplicata, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)))))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAnular)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEstornar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)
                        .addGap(13, 13, 13))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFatura)
                    .addComponent(lblFornecedor)
                    .addComponent(txtFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblIdFatura))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDataFatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDataFatura))
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblVencimento)
                            .addComponent(txtVencimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblVencimentoDesconto)
                            .addComponent(txtVencimentoDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDataPagamento)
                            .addComponent(lblNotaFiscal)
                            .addComponent(txtDataPagamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtReferencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblReferencia))
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtMontante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblMontante))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDesconto))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNotaFiscal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNotaFiscal1)
                            .addComponent(txtSerieNotaFiscal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDescricao)
                            .addComponent(lblStatus)
                            .addComponent(cmbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblDataPagamento1)
                        .addComponent(txtDuplicata, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(6, 6, 6)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnEstornar)
                    .addComponent(btnAnular)
                    .addComponent(btnSalvar))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void txtReferenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtReferenciaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_txtReferenciaActionPerformed

private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
    try {
        validaCampos();
        Fatura faturaPreenchida = preencheObjeto();
        
        try {
            if (lblIdFatura.isVisible() && 
                    Integer.parseInt(lblIdFatura.getText()) != 0){
                
                System.out.println("Setando fornecedor: " + lblIdFatura.getText());
                //seta o fornecedor para fazer update
                faturaPreenchida.setCdfatura(
                            Integer.parseInt(lblIdFatura.getText()));
            }
        } catch (Exception ignored){ /*do nothing*/ }
        
        new HibernateDAO<Fatura>().saveOrUpdate(faturaPreenchida);
        
        JOptionPane.showMessageDialog(null, "Salvo com sucesso", "Sucesso",
                    JOptionPane.INFORMATION_MESSAGE);
        
        FrmPrincipal.fecharAba(this, true);
        
        FrmPrincipal.getInstance().abreAba(new FrmFatura(faturaPreenchida), "Alt. Fatura");
        
    } catch (Exception ex){
        JOptionPane.showMessageDialog(null, "Campo " + ex.getMessage() + ", por favor revise", "Erro",
             JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
    }
}//GEN-LAST:event_btnSalvarActionPerformed

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
    FrmPrincipal.fecharAba(this, false);
}//GEN-LAST:event_btnCancelarActionPerformed

private void btnEstornarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEstornarActionPerformed
        try {
            fatura.setCdstatus("E");
            new HibernateDAO<Fatura>().saveOrUpdate(fatura);
            
            JOptionPane.showMessageDialog(null, "Estornado com sucesso", "Sucesso",
                    JOptionPane.INFORMATION_MESSAGE);
            
            FrmPrincipal.fecharAba(this, true);
            FrmPrincipal.getInstance().abreAba(new FrmFatura(fatura), "Alt. Fatura");//GEN-LAST:event_btnEstornarActionPerformed
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Erro ao estornar fatura, " +
                    "por favor relate este erro ao administrador da rede.", "Erro",
             JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();   
        }
}

private void btnAnularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnularActionPerformed
        try {
            fatura.setCdstatus("N");
            fatura.setDtpagamento(null);
            new HibernateDAO<Fatura>().saveOrUpdate(fatura);
            
            JOptionPane.showMessageDialog(null, "Anulado com sucesso", "Sucesso",
                    JOptionPane.INFORMATION_MESSAGE);
            
            FrmPrincipal.fecharAba(this, true);
            FrmPrincipal.getInstance().abreAba(new FrmFatura(fatura), "Alt. Fatura");//GEN-LAST:event_btnAnularActionPerformed
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Erro ao anular fatura, " +
                    "por favor relate este erro ao administrador da rede.", "Erro",
             JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
}


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnular;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEstornar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox cmbStatus;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDataFatura;
    private javax.swing.JLabel lblDataPagamento;
    private javax.swing.JLabel lblDataPagamento1;
    private javax.swing.JLabel lblDesconto;
    private javax.swing.JLabel lblDescricao;
    private javax.swing.JLabel lblFatura;
    private javax.swing.JLabel lblFornecedor;
    private javax.swing.JLabel lblIdFatura;
    private javax.swing.JLabel lblMontante;
    private javax.swing.JLabel lblNotaFiscal;
    private javax.swing.JLabel lblNotaFiscal1;
    private javax.swing.JLabel lblReferencia;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblVencimento;
    private javax.swing.JLabel lblVencimentoDesconto;
    private javax.swing.JTextArea txaDescricao;
    private javax.swing.JFormattedTextField txtDataFatura;
    private javax.swing.JFormattedTextField txtDataPagamento;
    private javax.swing.JTextField txtDesconto;
    private javax.swing.JTextField txtDuplicata;
    private javax.swing.JTextField txtFornecedor;
    private javax.swing.JTextField txtMontante;
    private javax.swing.JTextField txtNotaFiscal;
    private javax.swing.JTextField txtReferencia;
    private javax.swing.JTextField txtSerieNotaFiscal;
    private javax.swing.JFormattedTextField txtVencimento;
    private javax.swing.JFormattedTextField txtVencimentoDesconto;
    // End of variables declaration//GEN-END:variables

    private void validaCampos() throws Exception {
        Validator.existeId(Fornecedor.class, Parser.parseIntNullable(txtFornecedor.getText()), "Fornecedor");
        Validator.data(txtDataFatura.getText(), "Data da Fatura");
        Validator.monetario(txtMontante.getText(), "Montante");
        Validator.dataMinima(txtVencimento.getText(), txtDataFatura.getText(), "Data Vencimento");
        Validator.dataMinimaEmBranco(txtVencimentoDesconto.getText(), txtDataFatura.getText(), "Data Vencimento Desconto");
    }
    
    private Fatura preencheObjeto() throws Exception {
        Fatura fatura = new Fatura();
        fatura.setCdfornecedor(new Fornecedor(Integer.parseInt(txtFornecedor.getText())));
        fatura.setCdreferencia(txtReferencia.getText());
        fatura.setDtfatura(Parser.parseDateNullable(txtDataFatura.getText()));
        fatura.setVlmontante(Parser.parseFloatNullable(txtMontante.getText()));
        fatura.setDtvencimento(Parser.parseDateNullable(txtVencimento.getText()));
        fatura.setVldesconto(Parser.parseFloatNullable(txtDesconto.getText()));
        fatura.setDtfimdesconto(Parser.parseDateNullable(txtVencimentoDesconto.getText()));
        fatura.setDsfatura(txaDescricao.getText());
        fatura.setCdstatus((String) cmbStatus.getSelectedItem());
        return fatura;
    }
    
    private void preencheCampos(Fatura fatura) {
        try {
            lblIdFatura.setText(Parser.paraCampo(fatura.getCdfatura()));
            txtFornecedor.setText(Parser.paraCampo(fatura.getCdfornecedor()));
            txtReferencia.setText(Parser.paraCampo(fatura.getCdreferencia()));
            txtDataFatura.setText(Parser.formatDate(fatura.getDtfatura()));
            txtMontante.setText(Parser.paraCampo(fatura.getVlmontante()));
            txtVencimento.setText(Parser.formatDate(fatura.getDtvencimento()));
            txtDesconto.setText(Parser.paraCampo(fatura.getVldesconto()));
            txtVencimentoDesconto.setText(Parser.paraCampo(fatura.getDtfimdesconto()));
            txtNotaFiscal.setText(Parser.paraCampo(fatura.getCdnotafiscal()));
            txtDataPagamento.setText(Parser.paraCampo(fatura.getDtpagamento()));
            txtSerieNotaFiscal.setText(Parser.paraCampo(fatura.getCdserienotafiscal()));
            txtDuplicata.setText(Parser.paraCampo(fatura.getCdduplicata()));
            cmbStatus.setSelectedItem(Parser.paraCampo(fatura.getCdstatus()));
            System.out.println("cmbstatus " + fatura.getCdstatus());
            txaDescricao.setText(Parser.paraCampo(fatura.getDsfatura()));
            System.out.println("dsfatura " + fatura.getDsfatura());
        } catch (Exception e){
            //faz nada
            System.out.println("FrmFatura preencheCampos Exception");
            e.printStackTrace();
        }
    }
}
